import hashlib
import os
import time
import argparse

# 这是一个示例 Python 脚本。

# 按 Shift+F10 执行或将其替换为您的代码。
# 按 双击 Shift 在所有地方搜索类、文件、工具窗口、操作和设置。

def show_md5(dir_path):
    filenames = os.listdir(dir_path)
    for filename in filenames[:]:
        filepath = dir_path + '/' + filename
        if filepath.endswith('.png') or filepath.endswith('.jpg') or filepath.endswith('jpeg'):
            fd = open(filepath, "rb")
            f = fd.read()
            pmd5 = hashlib.md5(f)
            print(filename, pmd5.hexdigest())


def rmd5(dir_path):
    # dir_path = input("请输入图片路径：")
    writefile = int(time.time() * 1000)
    filenames = os.walk(dir_path)
    for root, dirs, files in filenames:
        for file in files:
            file = os.path.join(root, file)
            if file.endswith('.png') or file.endswith('.jpg') or file.endswith('jpeg'):
                with open(file, "rb") as fd:
                    f = fd.read()
                    pmd5 = hashlib.md5(f)
                    ff = file + " " + pmd5.hexdigest()

                with open(file, "a") as f:
                    f.write(str(writefile))

                with open(file, "rb") as fd:
                    f = fd.read()
                    pmd5 = hashlib.md5(f)
                    print(ff + " 改后：" + pmd5.hexdigest())
    # rmd5()


# 按间距中的绿色按钮以运行脚本。
if __name__ == '__main__':

    parser = argparse.ArgumentParser()

    # 添加参数解析
    parser.add_argument('-p', '--path', required=True, help='Input file')
    args = parser.parse_args()
    rmd5(args.path)

    exit(1)
    #ImageMD5.py -p /Users/zenglizhi/Desktop/Assets.xcassets
